package com.example.yasin.usersa;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.security.PrivateKey;
import java.security.PublicKey;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    EditText inputET,JiaET,JieET;
    Button JiaB,JieB;
    String JiamiString;
    private static String PublicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDEGYVBsV+fPrYXOjwIk6M7CejL\n" +
            "cw+WXbf6VRkgsrPLNrp9OA9tiwRsIxX+qkEoTMLpXYx8p4ERpnA8ZBG3LI/6QLoO\n" +
            "0g0JOzkF6CgIQZDKz8Ewn1a8a2PFgcwmX8kjDWZMnkJJdI/jvXvFB3kCG0t/+x7W\n" +
            "8XG6N5FaBUrlIjd/eQIDAQAB";
    private static String PrivateKey = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAMQZhUGxX58+thc6\n" +
            "PAiTozsJ6MtzD5Zdt/pVGSCys8s2un04D22LBGwjFf6qQShMwuldjHyngRGmcDxk\n" +
            "Ebcsj/pAug7SDQk7OQXoKAhBkMrPwTCfVrxrY8WBzCZfySMNZkyeQkl0j+O9e8UH\n" +
            "eQIbS3/7Htbxcbo3kVoFSuUiN395AgMBAAECgYAz7HH9YALqvocmhVPqDXWWJ1yN\n" +
            "qkzBStVxImFB6C91KmucxmBHaBzmqS+ku93+1rxxYETXqN6jFsf3JaQPNWx1pq3Z\n" +
            "VrjGh4GTbVpEf22YCuViK5pXGwWPNwdnQpVuFPeNZNn+DAfhfvXwToWtml1iRILV\n" +
            "ZH1lP1Kq7KfG/UzTFQJBAPh3TRzHCHAKUUCnjDL4qtZY5eV8qJ/Df0JnyI2cHzG/\n" +
            "aPmcTwGDUn3fhF1P5Z59z3EvDQBYexfRUh4vLefKpG8CQQDKC7rH3+mClXj3fpGo\n" +
            "vd6dDO3Bh8cIaUnS5KKY6UbMiTd1+wNf0+Fqdb6hId2vfkSCA1ogvM+z3cw5YycN\n" +
            "DJ6XAkAjT8Ezz82RLsYxbB5uuAx/fg7eOIM2gXA6nclQ5tlAFrXyk5Ze3q0Amp2T\n" +
            "LWGyzqqGqWstm2WTEvqas5XQlWodAkBwjsRJYUZXRyoSxuIinVQNLgyuChQpbW6B\n" +
            "HHowsZSNkcvh4CrrEcC8bEf8wD8d6zwDKhblsgoL6/4/4ir5LBSVAkBS5CpPtOpq\n" +
            "a99o18o0agmD3/G5nF2jlPmeiwlJDmo3wYmElQM3hOoAiSIoaA0i7z/SMkgsyruA\n" +
            "TTQJT9XVb9lq";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initLayoiut();
    }

    private void initLayoiut() {
        inputET = (EditText) findViewById(R.id.input);
        JiaET = (EditText) findViewById(R.id.jiamitext);
        JieET = (EditText) findViewById(R.id.jiemitext);
        JiaB = (Button) findViewById(R.id.jiami);
        JiaB.setOnClickListener(this);
        JieB = (Button) findViewById(R.id.jiemi);
        JieB.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.jiami:
                String source = inputET.getText().toString().trim();

                try {
                    java.security.PublicKey publicKey = RSAUtils.loadPublicKey(PublicKey);
                    byte[] encryptByte = RSAUtils.encryptData(source.getBytes(), publicKey);
                    // 为了方便观察吧加密后的数据用base64加密转一下，要不然看起来是乱码,所以解密是也是要用Base64先转换
                    String afterencrypt = Base64Utils.encode(encryptByte);
                    JiaET.setText(afterencrypt);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.jiemi:
                String encryptContent = JiaET.getText().toString().trim();
                try
                {
                    // 从字符串中得到私钥
                    PrivateKey privateKey = RSAUtils.loadPrivateKey(PrivateKey);
                    // 从文件中得到私钥
//                    InputStream inPrivate = getResources().getAssets().open("pkcs8_rsa_private_key.pem");
//                    PrivateKey privateKey = RSAUtils.loadPrivateKey(inPrivate);
                    // 因为RSA加密后的内容经Base64再加密转换了一下，所以先Base64解密回来再给RSA解密
                    byte[] decryptByte = RSAUtils.decryptData(Base64Utils.decode(encryptContent), privateKey);
                    String decryptStr = new String(decryptByte);
                    JieET.setText(decryptStr);
                } catch (Exception e)
                {
                    e.printStackTrace();
                }

                break;
        }
    }
}
